home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-11-01 | 55.5 KB | 1,344 lines |
- Newsgroups: comp.sources.misc
- From: daveg@synaptics.com (David Gillespie)
- Subject: v24i090: gnucalc - GNU Emacs Calculator, v2.00, Part42/56
- Message-ID: <1991Nov1.183709.20885@sparky.imd.sterling.com>
- X-Md4-Signature: 89017c936cce42a4c6e60716bbf12c30
- Date: Fri, 1 Nov 1991 18:37:09 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: daveg@synaptics.com (David Gillespie)
- Posting-number: Volume 24, Issue 90
- Archive-name: gnucalc/part42
- Environment: Emacs
- Supersedes: gmcalc: Volume 13, Issue 27-45
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file calc.texinfo continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 42; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping calc.texinfo'
- else
- echo 'x - continuing file calc.texinfo'
- sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
- lump-sum argument to these functions, but the computation will be
- rather slow in the four-argument case.@refill
- X
- @kindex H b #
- @tindex nperl
- The @kbd{H b #} [@code{nperl}] command does the same computation
- using @code{pvl}. By exchanging @var{payment} and @var{amount} you
- can also get the solution for @code{fvl}. For example,
- @code{nperl(8%, 2000, 1000) = 9.006}, so if you place $1000 in a
- bank account earning 8%, it will take nine years to grow to $2000.@refill
- X
- @kindex b T
- @pindex calc-fin-rate
- @tindex rate
- The @kbd{b T} (@code{calc-fin-rate}) [@code{rate}] command computes
- the rate of return on an investment. This is also an inverse of @code{pv}:
- @code{rate(@var{n}, @var{payment}, @var{amount})} computes the value of
- @var{rate} such that @code{pv(@var{rate}, @var{n}, @var{payment}) =
- @var{amount}}.@refill
- X
- @kindex I b T
- @tindex H b T
- @tindex rateb
- @tindex ratel
- The @kbd{I b T} [@code{rateb}] and @kbd{H b T} [@code{ratel}]
- commands solve the analogous equations with @code{pvb} or @code{pvl}
- in place of @code{pv}. Also, @code{rate} and @code{rateb} can
- accept an optional fourth argument just like @code{pv} and @code{pvb}.
- To redo the above example from a different perspective,
- @code{ratel(9, 2000, 1000) = .08}, which says you will need an
- interest rate of 8% in order to double your account in nine years.@refill
- X
- @kindex b I
- @pindex calc-fin-irr
- @tindex irr
- The @kbd{b I} (@code{calc-fin-irr}) [@code{irr}] command is the
- analogous function to @code{rate} but for net present value.
- Its argument is a vector of payments. Thus @code{irr(@var{payments})}
- computes the @var{rate} such that @code{npv(@var{rate}, @var{payments}) = 0};
- this rate is known as the @dfn{internal rate of return}.
- X
- @kindex I b I
- @tindex irrb
- The @kbd{I b I} [@code{irrb}] command computes the internal rate of
- return assuming payments occur at the beginning of each period.
- X
- @node Depreciation Functions, Definitions of Financial Functions, Related Financial Functions, Financial Functions
- @subsection Depreciation Functions
- X
- @noindent
- The functions in this section calculate @dfn{depreciation}, which is
- the amount of value that a possession loses over time. These functions
- are characterized by three parameters: @var{cost}, the original cost
- of the asset; @var{salvage}, the value the asset will have at the end
- of its expected ``useful life''; and @var{life}, the number of years
- (or other periods) of the expected useful life.
- X
- There are several methods for calculating depreciation that differ in
- the way they spread the depreciation over the lifetime of the asset.
- X
- @kindex b S
- @pindex calc-fin-sln
- @tindex sln
- The @kbd{b S} (@code{calc-fin-sln}) [@code{sln}] command computes the
- ``straight-line'' depreciation. In this method, the asset depreciates
- by the same amount every year (or period). For example,
- @samp{sln(12000, 2000, 5)} returns 2000. The asset costs $12000
- initially and will be worth $2000 after five years; it loses $2000
- per year.
- X
- @kindex b Y
- @pindex calc-fin-syd
- @tindex syd
- The @kbd{b Y} (@code{calc-fin-syd}) [@code{syd}] command computes the
- accelerated ``sum-of-years'-digits'' depreciation. Here the depreciation
- is higher during the early years of the asset's life. Since the
- depreciation is different each year, @kbd{b Y} takes a fourth @var{period}
- parameter which specifies which year is requested, from 1 to @var{life}.
- If @var{period} is outside this range, the @code{syd} function will
- return zero.
- X
- @kindex b D
- @pindex calc-fin-ddb
- @tindex ddb
- The @kbd{b D} (@code{calc-fin-ddb}) [@code{ddb}] command computes an
- accelerated depreciation using the double-declining balance method.
- It also takes a fourth @var{period} parameter.
- X
- For symmetry, the @code{sln} function will accept a @var{period}
- parameter as well, although it will ignore its value except that the
- return value will as usual be zero if @var{period} is out of range.
- X
- For example, pushing the vector @cite{[1,2,3,4,5]} (using @kbd{v x 5})
- and then mapping @kbd{V M ' [sln(12000,2000,5,$), syd(12000,2000,5,$),
- ddb(12000,2000,5,$)] RET} produces a matrix that allows us to compare
- the three depreciation methods:
- X
- @group
- @example
- [ [ 2000, 3333, 4800 ]
- X [ 2000, 2667, 2880 ]
- X [ 2000, 2000, 1728 ]
- X [ 2000, 1333, 592 ]
- X [ 2000, 667, 0 ] ]
- @end example
- @end group
- X
- @noindent
- (Values have been rounded to nearest integers in this figure.)
- We see that @code{sln} depreciates by the same amount each year,
- @kbd{syd} depreciates more at the beginning and less at the end,
- and @kbd{ddb} weights the depreciation even more toward the beginning.
- X
- Summing columns with @kbd{V R : +} yields @cite{[10000, 10000, 10000]};
- the total depreciation in any method is (by definition) the
- difference between the cost and the salvage value.
- X
- @node Definitions of Financial Functions, , Depreciation Functions, Financial Functions
- @subsection Definitions
- X
- @noindent
- For your reference, here are the actual formulas used to compute
- Calc's financial functions.
- X
- Calc will not evaluate a financial function unless the @var{rate} or
- @var{n} argument is known. However, @var{payment} or @var{amount} can
- be a variable. Calc expands these functions according to the
- formulas below for symbolic arguments only when you use the @kbd{a "}
- (@code{calc-expand-formula}) command, or when taking derivatives or
- integrals or solving equations involving the functions.
- X
- @ifinfo
- These formulas are shown using the conventions of ``Big'' display
- mode (@kbd{d B}); for example, the formula for @code{fv} written
- linearly is @samp{pmt * ((1 + rate)^n) - 1) / rate}.
- X
- @example
- X n
- X (1 + rate) - 1
- fv(rate, n, pmt) = pmt * ---------------
- X rate
- X
- X n
- X ((1 + rate) - 1) (1 + rate)
- fvb(rate, n, pmt) = pmt * ----------------------------
- X rate
- X
- X n
- fvl(rate, n, pmt) = pmt * (1 + rate)
- X
- X -n
- X 1 - (1 + rate)
- pv(rate, n, pmt) = pmt * ----------------
- X rate
- X
- X -n
- X (1 - (1 + rate) ) (1 + rate)
- pvb(rate, n, pmt) = pmt * -----------------------------
- X rate
- X
- X -n
- pvl(rate, n, pmt) = pmt * (1 + rate)
- X
- X -1 -2 -3
- npv(rate, [a, b, c]) = a*(1 + rate) + b*(1 + rate) + c*(1 + rate)
- X
- X -1 -2
- npvb(rate, [a, b, c]) = a + b*(1 + rate) + c*(1 + rate)
- X
- X -n
- X (amt - x * (1 + rate) ) * rate
- pmt(rate, n, amt, x) = -------------------------------
- X -n
- X 1 - (1 + rate)
- X
- X -n
- X (amt - x * (1 + rate) ) * rate
- pmtb(rate, n, amt, x) = -------------------------------
- X -n
- X (1 - (1 + rate) ) (1 + rate)
- X
- X amt * rate
- nper(rate, pmt, amt) = - log(1 - ------------, 1 + rate)
- X pmt
- X
- X amt * rate
- nperb(rate, pmt, amt) = - log(1 - ---------------, 1 + rate)
- X pmt * (1 + rate)
- X
- X amt
- nperl(rate, pmt, amt) = - log(---, 1 + rate)
- X pmt
- X
- X 1/n
- X pmt
- ratel(n, pmt, amt) = ------ - 1
- X 1/n
- X amt
- X
- X cost - salv
- sln(cost, salv, life) = -----------
- X life
- X
- X (cost - salv) * (life - per + 1)
- syd(cost, salv, life, per) = --------------------------------
- X life * (life + 1) / 2
- X
- X book * 2
- ddb(cost, salv, life, per) = --------, book = cost - depreciation so far
- X life
- @end example
- @end ifinfo
- @tex
- \turnoffactive
- $$ \code{fv}(r, n, p) = p { (1 + r)^n - 1 \over r } $$
- $$ \code{fvb}(r, n, p) = p { ((1 + r)^n - 1) (1 + r) \over r } $$
- $$ \code{fvl}(r, n, p) = p (1 + r)^n $$
- $$ \code{pv}(r, n, p) = p { 1 - (1 + r)^{-n} \over r } $$
- $$ \code{pvb}(r, n, p) = p { (1 - (1 + r)^{-n}) (1 + r) \over r } $$
- $$ \code{pvl}(r, n, p) = p (1 + r)^{-n} $$
- $$ \code{npv}(r, [a,b,c]) = a (1 + r)^{-1} + b (1 + r)^{-2} + c (1 + r)^{-3} $$
- $$ \code{npvb}(r, [a,b,c]) = a + b (1 + r)^{-1} + c (1 + r)^{-2} $$
- $$ \code{pmt}(r, n, a, x) = { (a - x (1 + r)^{-n}) r \over 1 - (1 + r)^{-n} }$$
- $$ \code{pmtb}(r, n, a, x) = { (a - x (1 + r)^{-n}) r \over
- X (1 - (1 + r)^{-n}) (1 + r) } $$
- $$ \code{nper}(r, p, a) = -\code{log}(1 - { a r \over p }, 1 + r) $$
- $$ \code{nperb}(r, p, a) = -\code{log}(1 - { a r \over p (1 + r) }, 1 + r) $$
- $$ \code{nperl}(r, p, a) = -\code{log}({a \over p}, 1 + r) $$
- $$ \code{ratel}(n, p, a) = { p^{1/n} \over a^{1/n} } - 1 $$
- $$ \code{sln}(c, s, l) = { c - s \over l } $$
- $$ \code{syd}(c, s, l, p) = { (c - s) (l - p + 1) \over l (l+1) / 2 } $$
- $$ \code{ddb}(c, s, l, p) = { 2 (c - \hbox{depreciation so far}) \over l } $$
- @end tex
- X
- In @code{pmt} and @code{pmtb}, @cite{x=0} if omitted.
- X
- These functions accept any numeric objects, including error forms,
- intervals, and even (though not very usefully) complex numbers. The
- above formulas specify exactly the behavior of these functions with
- all sorts of inputs.
- X
- Note that if the first argument to the @code{log} in @code{nper} is
- negative, @code{nper} leaves itself in symbolic form rather than
- returning a (financially meaningless) complex number.
- X
- @samp{rate(num, pmt, amt)} solves the equation
- @samp{pv(rate, num, pmt) = amt} for @samp{rate} using @kbd{H a R}
- (@code{calc-find-root}), with the interval @samp{[.01% .. 100%]}
- for an initial guess. The @code{rateb} function is the same except
- that it uses @code{pvb}. Note that @code{ratel} can be solved
- directly; its formula is shown in the above list.
- X
- Similarly, @samp{irr(pmts)} solves the equation @samp{npv(rate, pmts) = 0}
- for @samp{rate}.
- X
- If you give a fourth argument to @code{nper} or @code{nperb}, Calc
- will also use @kbd{H a R} to solve the equation using an initial
- guess interval of @samp{[0 .. 100]}.
- X
- A fourth argument to @code{fv} simply sums the two components
- calculated from the above formulas for @code{fv} and @code{fvl}.
- The same is true of @code{fvb}, @code{pv}, and @code{pvb}.
- X
- The @kbd{ddb} function is computed iteratively; the ``book'' value
- starts out equal to @var{cost}, and decreases according to the above
- formula for the specified number of periods. If the book value
- would decrease below @var{salvage}, it only decreases to @var{salvage}
- and the depreciation is zero for all subsequent periods. The @code{ddb}
- function returns the amount the book value decreased in the specified
- period.
- X
- The Calc financial function names were borrowed mostly from Microsoft
- Excel and Borland's Quattro. The @code{ratel} function corresponds to
- @samp{@@CGR} in Borland's Reflex. The @code{nper} and @code{nperl}
- functions correspond to @samp{@@TERM} and @samp{@@CTERM} in Quattro,
- respectively. Beware that the Calc functions may take their arguments
- in a different order than your favorite spreadsheet.
- X
- @node Binary Functions, , Financial Functions, Arithmetic
- @section Binary Number Functions
- X
- @noindent
- The commands in this chapter all use two-letter sequences beginning with
- the @kbd{b} prefix.
- X
- @cindex Binary numbers
- The ``binary'' operations actually work regardless of the currently
- displayed radix, although their results make the most sense in a radix
- like 2, 8, or 16 (as obtained by the @kbd{d 2}, @kbd{d 8}, or @kbd{d 6}
- commands, respectively). You may also wish to enable display of leading
- zeros with @kbd{d z}. @xref{Radix Modes}.
- X
- @cindex Word size for binary operations
- The Calculator maintains a current @dfn{word size} @cite{w}, an
- arbitrary positive or negative integer. For a positive word size, all
- of the binary operations described here operate modulo @cite{2^w}. In
- particular, negative arguments are converted to positive integers modulo
- @cite{2^w} by all binary functions.@refill
- X
- If the word size is negative, binary operations produce 2's complement
- integers from @c{$-2^{-w-1}$}
- @cite{-(2^(-w-1))} to @c{$2^{-w-1}-1$}
- @cite{2^(-w-1)-1} inclusive. Either
- mode accepts inputs in any range; the sign of @cite{w} affects only
- the results produced.
- X
- @kindex b c
- @pindex calc-clip
- @tindex clip
- The @kbd{b c} (@code{calc-clip})
- [@code{clip}] command can be used to clip a number by reducing it modulo
- @cite{2^w}. The commands described in this chapter automatically clip
- their results to the current word size. Note that other operations like
- addition do not use the current word size, since integer addition
- generally is not ``binary.'' (However, @pxref{Simplification Modes},
- @code{calc-bin-simplify-mode}.) For example, with a word size of 8
- bits @kbd{b c} converts a number to the range 0 to 255; with a word
- size of -8 @kbd{b c} converts to the range -128 to 127.@refill
- X
- @kindex b w
- @pindex calc-word-size
- The default word size is 32 bits. All operations except the shifts and
- rotates allow you to specify a different word size for that one
- operation by giving a numeric prefix argument: @kbd{C-u 8 b c} clips the
- top of stack to the range 0 to 255 regardless of the current word size.
- To set the word size permanently, use @kbd{b w} (@code{calc-word-size}).
- This command displays a prompt with the current word size; press @key{RET}
- immediately to keep this word size, or type a new word size at the prompt.
- X
- When the binary operations are written in symbolic form, they take an
- optional second (or third) word-size parameter. When a formula like
- @samp{and(a,b)} is finally evaluated, the word size current at that time
- will be used, but when @samp{and(a,b,-8)} is evaluated, a word size of
- @i{-8} will always be used. A symbolic binary function will be left
- in symbolic form unless the all of its argument(s) are integers or
- integer-valued floats.
- X
- If either or both arguments are modulo forms for which @cite{M} is a
- power of two, that power of two is taken as the word size unless a
- numeric prefix argument overrides it. The current word size is never
- consulted when modulo-power-of-two forms are involved.
- X
- @kindex b a
- @pindex calc-and
- @tindex and
- The @kbd{b a} (@code{calc-and}) [@code{and}] command computes the bitwise
- AND of the two numbers on the top of the stack. In other words, for each
- of the @cite{w} binary digits of the two numbers (pairwise), the corresponding
- bit of the result is 1 if and only if both input bits are 1:
- @samp{and(2#1100, 2#1010) = 2#1000}.
- X
- @kindex b o
- @pindex calc-or
- @tindex or
- The @kbd{b o} (@code{calc-or}) [@code{or}] command computes the bitwise
- inclusive OR of two numbers. A bit is 1 if either of the input bits, or
- both, are 1: @samp{or(2#1100, 2#1010) = 2#1110}.
- X
- @kindex b x
- @pindex calc-xor
- @tindex xor
- The @kbd{b x} (@code{calc-xor}) [@code{xor}] command computes the bitwise
- exclusive OR of two numbers. A bit is 1 if exactly one of the input bits
- is 1: @samp{xor(2#1100, 2#1010) = 2#0110}.
- X
- @kindex b d
- @pindex calc-diff
- @tindex diff
- The @kbd{b d} (@code{calc-diff}) [@code{diff}] command computes the bitwise
- difference of two numbers; this is defined by @samp{diff(a,b) = and(a,not(b))},
- so that @samp{diff(2#1100, 2#1010) = 2#0100}.
- X
- @kindex b n
- @pindex calc-not
- @tindex not
- The @kbd{b n} (@code{calc-not}) [@code{not}] command computes the bitwise
- NOT of a number. A bit is 1 if the input bit is 0 and vice-versa.
- X
- @kindex b l
- @pindex calc-lshift-binary
- @tindex lsh
- The @kbd{b l} (@code{calc-lshift-binary}) [@code{lsh}] command shifts a
- number left by one bit, or by the number of bits specified in the numeric
- prefix argument. A negative prefix argument performs a logical right shift,
- in which zeros are shifted in on the left. In symbolic form, @samp{lsh(a)}
- is short for @samp{lsh(a,1)}, which in turn is short for @samp{lsh(a,n,w)}.
- Bits shifted ``off the end,'' according to the current word size, are lost.
- X
- @kindex H b l
- The @kbd{H b l} command also does a left shift, but it takes two arguments
- from the stack (the value to shift, and, at top-of-stack, the number of
- bits to shift). This version interprets the prefix argument just like
- the regular binary operations, i.e., as a word size. The Hyperbolic flag
- has a similar effect on the rest of the binary shift and rotate commands.
- X
- @kindex b r
- @kindex H b r
- @pindex calc-rshift-binary
- @tindex rsh
- The @kbd{b r} (@code{calc-rshift-binary}) [@code{rsh}] command shifts a
- number right by one bit, or by the number of bits specified in the numeric
- prefix argument: @samp{rsh(a,n) = lsh(a,-n)}.
- X
- @kindex b L
- @kindex H b L
- @pindex calc-lshift-arith
- @tindex ash
- The @kbd{b L} (@code{calc-lshift-arith}) [@code{ash}] command shifts a
- number left. It is analogous to @code{lsh}, except that if the shift
- is rightward (the prefix argument is negative), an arithmetic shift
- is performed as described below.
- X
- @kindex b R
- @kindex H b R
- @pindex calc-rshift-arith
- @tindex rash
- The @kbd{b R} (@code{calc-rshift-arith}) [@code{rash}] command performs
- an ``arithmetic'' shift to the right, in which the leftmost bit (according
- to the current word size) is duplicated rather than shifting in zeros.
- This corresponds to dividing by a power of two where the input is interpreted
- as a signed, twos-complement number. (The distinction between the @samp{rsh}
- and @samp{rash} operations is totally independent from whether the word
- size is positive or negative.) With a negative prefix argument, this
- performs a standard left shift.
- X
- @kindex b t
- @kindex H b t
- @pindex calc-rotate-binary
- @tindex rot
- The @kbd{b t} (@code{calc-rotate-binary}) [@code{rot}] command rotates a
- number one bit to the left. The leftmost bit (according to the current
- word size) is dropped off the left and shifted in on the right. With a
- numeric prefix argument, the number is rotated that many bits to the left
- or right.
- X
- @xref{Set Operations}, for the @kbd{b p} and @kbd{b u} commands that
- pack and unpack binary integers into sets. (For example, @kbd{b u}
- unpacks the number @samp{2#11001} to the set of bit-numbers
- @samp{[0, 3, 4]}.) Type @kbd{b u V #} to count the number of ``1''
- bits in a binary integer.
- X
- Another interesting use of the set representation of binary integers
- is to reverse the bits in, say, a 32-bit integer. Type @kbd{b u} to
- unpack; type @kbd{31 TAB -} to replace each bit-number in the set
- with 31 minus that bit-number; type @kbd{b p} to pack the set back
- into a binary integer.
- X
- @node Scientific Functions, Matrix Functions, Arithmetic, Top
- @chapter Scientific Functions
- X
- @noindent
- The functions described here perform trigonometric and other transcendental
- calculations. They generally produce floating-point answers correct to the
- full current precision. The @kbd{H} (Hyperbolic) and @kbd{I} (Inverse)
- flag keys must be used to get some of these functions from the keyboard.
- X
- @kindex P
- @pindex calc-pi
- @cindex @code{pi} variable
- @vindex pi
- @kindex H P
- @cindex @code{e} variable
- @vindex e
- @kindex I P
- @cindex @code{gamma} variable
- @vindex gamma
- @cindex Gamma constant, Euler's
- @cindex Euler's gamma constant
- @kindex H I P
- @cindex @code{phi} variable
- @cindex Phi, golden ratio
- @cindex Golden ratio
- One miscellanous command is shift-@kbd{P} (@code{calc-pi}), which pushes
- the value of @c{$\pi$}
- @cite{pi} (at the current precision) onto the stack. With the
- Hyperbolic flag, it pushes the value @cite{e}, the base of natural logarithms.
- With the Inverse flag, it pushes Euler's constant @c{$\gamma$}
- @cite{gamma} (about 0.577). With both Inverse and Hyperbolic, it
- pushes the ``golden ratio'' @c{$\phi$}
- @cite{phi} (about 1.618). (At present, Euler's constant is not available
- to unlimited precision; Calc knows only the first 100 digits.)
- In Symbolic mode, these commands push the
- actual variables @samp{pi}, @samp{e}, @samp{gamma}, and @samp{phi},
- respectively, instead of their values; @pxref{Symbolic Mode}.@refill
- X
- @tindex sqr
- The @kbd{Q} (@code{calc-sqrt}) [@code{sqrt}] function is described elsewhere;
- @pxref{Basic Arithmetic}. With the Inverse flag [@code{sqr}], this command
- computes the square of the argument.
- X
- @xref{Prefix Arguments}, for a discussion of the effect of numeric
- prefix arguments on commands in this chapter which do not otherwise
- interpret a prefix argument.
- X
- @menu
- * Logarithmic Functions::
- * Trigonometric and Hyperbolic Functions::
- * Advanced Math Functions::
- * Branch Cuts::
- * Random Numbers::
- * Combinatorial Functions::
- * Probability Distribution Functions::
- @end menu
- X
- @node Logarithmic Functions, Trigonometric and Hyperbolic Functions, Scientific Functions, Scientific Functions
- @section Logarithmic Functions
- X
- @noindent
- @kindex L
- @kindex I E
- @pindex calc-ln
- @tindex ln
- The shift-@kbd{L} (@code{calc-ln}) [@code{ln}] command computes the natural
- logarithm of the real or complex number on the top of the stack. With
- the Inverse flag it computes the exponential function instead, although
- this is redundant with the @kbd{E} command.
- X
- @kindex E
- @kindex I L
- @pindex calc-exp
- @tindex exp
- The shift-@kbd{E} (@code{calc-exp}) [@code{exp}] command computes the
- exponential, i.e., @cite{e} raised to the power of the number on the stack.
- The meanings of the Inverse and Hyperbolic flags follow from those for
- the @code{calc-ln} command.
- X
- @kindex H L
- @kindex H I L
- @kindex H E
- @kindex H I E
- @pindex calc-log10
- @tindex log10
- @tindex exp10
- The @kbd{H L} (@code{calc-log10}) [@code{log10}] command computes the common
- (base-10) logarithm of a number. The meanings of the Inverse [@code{exp10}]
- and Hyperbolic flags follow from those for the @kbd{L} command. Note
- that the common logarithm of a complex number is computed by taking the
- natural logarithm and dividing by @c{$\ln10$}
- @cite{ln(10)}.
- X
- @kindex B
- @kindex I B
- @pindex calc-log
- @tindex log
- @tindex alog
- The @kbd{B} (@code{calc-log}) [@code{log}] command computes a logarithm
- to any base. For example, @kbd{1024 @key{RET} 2 B} produces 10, since
- @c{$2^{10} = 1024$}
- @cite{2^10 = 1024}. In certain cases like @samp{log(3,9)}, the result
- will be either @cite{1:2} or @cite{0.5} depending on the current Fraction
- Mode setting. With the Inverse flag [@code{alog}], this command is
- similar to @kbd{^} except that the order of the arguments is reversed.
- X
- @kindex f I
- @pindex calc-ilog
- @tindex ilog
- The @kbd{f I} (@code{calc-ilog}) [@code{ilog}] command computes the
- integer logarithm of a number to any base. The number and the base must
- themselves be positive integers. This is the true logarithm, rounded
- down to an integer. Thus @kbd{ilog(x,10)} is 3 for all @cite{x} in the
- range from 1000 to 9999. If both arguments are positive integers, exact
- integer arithmetic is used; otherwise, this is equivalent to
- @samp{floor(log(x,b))}.
- X
- @kindex f E
- @pindex calc-expm1
- @tindex expm1
- The @kbd{f E} (@code{calc-expm1}) [@code{expm1}] command computes
- @c{$e^x - 1$}
- @cite{exp(x)-1}, but using an algorithm that produces a more accurate
- answer when the result is close to zero, i.e., when @c{$e^x$}
- @cite{exp(x)} is close
- to one.
- X
- @kindex f L
- @pindex calc-lnp1
- @tindex lnp1
- The @kbd{f L} (@code{calc-lnp1}) [@code{lnp1}] command computes
- @c{$\ln(x+1)$}
- @cite{ln(x+1)}, producing a more accurate answer when @cite{x} is close
- to zero.
- X
- @node Trigonometric and Hyperbolic Functions, Advanced Math Functions, Logarithmic Functions, Scientific Functions
- @section Trigonometric/Hyperbolic Functions
- X
- @noindent
- @kindex S
- @pindex calc-sin
- @tindex sin
- The shift-@kbd{S} (@code{calc-sin}) [@code{sin}] command computes the sine
- of an angle or complex number. If the input is an HMS form, it is interpreted
- as degrees-minutes-seconds; otherwise, the input is interpreted according
- to the current angular mode. It is best to use Radians mode when operating
- on complex numbers.@refill
- X
- Calc's ``units'' mechanism includes angular units like @code{deg},
- @code{rad}, and @code{grad}. While @samp{sin(45 deg)} is not evaluated
- all the time, the @kbd{u s} (@code{calc-simplify-units}) command will
- simplify @samp{sin(45 deg)} by taking the sine of 45 degrees, regardless
- of the current angular mode. @xref{Basic Operations on Units}.
- X
- Also, the symbolic variable @code{pi} is not ordinarily recognized in
- arguments to trigonometric functions, as in @samp{sin(3 pi / 4)}, but
- the @kbd{a s} (@code{calc-simplify}) command recognizes many such
- formulas when the current angular mode is radians @emph{and} symbolic
- mode is enabled; this example would be replaced by @samp{sqrt(2) / 2}.
- @xref{Symbolic Mode}. Beware, this simplification occurs even if you
- have stored a different value in the variable @samp{pi}; this is one
- reason why changing built-in variables is a bad idea. Arguments of
- the form @cite{x} plus a multiple of @c{$\pi/2$}
- @cite{pi/2} are also simplified.
- Calc includes similar formulas for @code{cos} and @code{tan}.@refill
- X
- The @kbd{a s} command knows all angles which are integer multiples of
- @c{$\pi/12$}
- @cite{pi/12}, @c{$\pi/10$}
- @cite{pi/10}, or @c{$\pi/8$}
- @cite{pi/8} radians. In degrees mode,
- analogous simplifications occur for integer multiples of 15 or 18
- degrees, and for arguments plus multiples of 90 degrees.
- X
- @kindex I S
- @pindex calc-arcsin
- @tindex arcsin
- With the Inverse flag, @code{calc-sin} computes an arcsine. This is also
- available as the @code{calc-arcsin} command or @code{arcsin} algebraic
- function. The returned argument is converted to degrees, radians, or HMS
- notation depending on the current angular mode.
- X
- @kindex H S
- @pindex calc-sinh
- @tindex sinh
- @kindex H I S
- @pindex calc-arcsinh
- @tindex arcsinh
- With the Hyperbolic flag, @code{calc-sin} computes the hyperbolic
- sine, also available as @code{calc-sinh} [@code{sinh}]. With the
- Hyperbolic and Inverse flags, it computes the hyperbolic arcsine
- (@code{calc-arcsinh}) [@code{arcsinh}].
- X
- @kindex C
- @pindex calc-cos
- @tindex cos
- @kindex I C
- @pindex calc-arccos
- @tindex arccos
- @kindex H C
- @pindex calc-cosh
- @tindex cosh
- @kindex H I C
- @pindex calc-arccosh
- @tindex arccosh
- @kindex T
- @pindex calc-tan
- @tindex tan
- @kindex I T
- @pindex calc-arctan
- @tindex arctan
- @kindex H T
- @pindex calc-tanh
- @tindex tanh
- @kindex H I T
- @pindex calc-arctanh
- @tindex arctanh
- The shift-@kbd{C} (@code{calc-cos}) [@code{cos}] command computes the cosine
- of an angle or complex number, and shift-@kbd{T} (@code{calc-tan}) [@code{tan}]
- computes the tangent, along with all the various inverse and hyperbolic
- variants of these functions.
- X
- @kindex f T
- @pindex calc-arctan2
- @tindex arctan2
- The @kbd{f T} (@code{calc-arctan2}) [@code{arctan2}] command takes two
- numbers from the stack and computes the arc tangent of their ratio. The
- result is in the full range from @i{-180} (exclusive) to @i{+180}
- (inclusive) degrees, or the analogous range in radians. A similar
- result would be obtained with @kbd{/} followed by @kbd{I T}, but the
- value would only be in the range from @i{-90} to @i{+90} degrees
- since the division loses information about the signs of the two
- components, and an error might result from an explicit division by zero
- which @code{arctan2} would avoid. By (arbitrary) definition,
- @samp{arctan2(0,0)=0}.
- X
- @pindex calc-sincos
- @tindex sincos
- @tindex arcsincos
- The @code{calc-sincos} [@code{sincos}] command computes the sine and
- cosine of a number, returning them as a vector of the form
- @samp{[@var{cos}, @var{sin}]}.
- With the Inverse flag [@code{arcsincos}], this command takes a two-element
- vector as an argument and computes @code{arctan2} of the elements.
- (This command does not accept the Hyperbolic flag.)@refill
- X
- @node Advanced Math Functions, Branch Cuts, Trigonometric and Hyperbolic Functions, Scientific Functions
- @section Advanced Mathematical Functions
- X
- @noindent
- Calc can compute a variety of less common functions that arise in
- various branches of mathematics. All of the functions described in
- this section allow arbitrary complex arguments and, except as noted,
- will work to arbitrarily large precisions. They can not at present
- handle error forms or intervals as arguments.
- X
- NOTE: These functions are still experimental. In particular, their
- accuracy is not guaranteed in all domains. It is advisable to set the
- current precision comfortably higher than you actually need when
- using these functions. Also, these functions may be impractically
- slow for some values of the arguments.
- X
- @kindex f g
- @pindex calc-gamma
- @tindex gamma
- The @kbd{f g} (@code{calc-gamma}) [@code{gamma}] command computes the Euler
- gamma function. For positive integer arguments, this is related to the
- factorial function: @samp{gamma(n+1) = fact(n)}. For general complex
- arguments the gamma function can be defined by the following definite
- integral: @c{$\Gamma(a) = \int_0^\infty t^{a-1} e^t dt$}
- @cite{gamma(a) = integ(t^(a-1) exp(t), t, 0, inf)}.
- (The actual implementation uses far more efficient computational methods.)
- X
- @kindex f G
- @kindex I f G
- @kindex H f G
- @kindex H I f G
- @pindex calc-inc-gamma
- @tindex gammaP
- @tindex gammaQ
- @tindex gammag
- @tindex gammaG
- The @kbd{f G} (@code{calc-inc-gamma}) [@code{gammaP}] command computes
- the incomplete gamma function, denoted @samp{P(a,x)}. This is defined by
- the integral, @c{$P(a,x) = \left( \int_0^x t^{a-1} e^t dt \right) / \Gamma(a)$}
- @cite{gammaP(a,x) = integ(t^(a-1) exp(t), t, 0, x) / gamma(a)}.
- This implies that @samp{gammaP(a,inf) = 1} for any @cite{a} (see the
- definition of the normal gamma function).
- X
- Several other varieties of incomplete gamma function are defined.
- The complement of @cite{P(a,x)}, called @cite{Q(a,x) = 1-P(a,x)} by
- some authors, is computed by the @kbd{I f G} [@code{gammaQ}] command.
- You can think of this as taking the other half of the integral, from
- @cite{x} to infinity.
- X
- @ifinfo
- The functions corresponding to the integrals that define @cite{P(a,x)}
- and @cite{Q(a,x)} but without the normalizing @cite{1/gamma(a)}
- factor are called @cite{g(a,x)} and @cite{G(a,x)}, respectively
- (where @cite{g} and @cite{G} represent the lower- and upper-case Greek
- letter gamma). You can obtain these using the @kbd{H f G} [@code{gammag}]
- and @kbd{H I f G} [@code{gammaG}] commands.
- @end ifinfo
- @tex
- \turnoffactive
- The functions corresponding to the integrals that define $P(a,x)$
- and $Q(a,x)$ but without the normalizing $1/\Gamma(a)$
- factor are called $\gamma(a,x)$ and $\Gamma(a,x)$, respectively.
- You can obtain these using the \kbd{H f G} [\code{gammag}] and
- \kbd{I H f G} [\code{gammaG}] commands.
- @end tex
- X
- @kindex f b
- @pindex calc-beta
- @tindex beta
- The @kbd{f b} (@code{calc-beta}) [@code{beta}] command computes the
- Euler beta function, which is defined in terms of the gamma function as
- @c{$B(a,b) = \Gamma(a) \Gamma(b) / \Gamma(a+b)$}
- @cite{beta(a,b) = gamma(a) gamma(b) / gamma(a+b)}, or by
- @c{$B(a,b) = \int_0^1 t^{a-1} (1-t)^{b-1} dt$}
- @cite{beta(a,b) = integ(t^(a-1) (1-t)^(b-1), t, 0, 1)}.
- X
- @kindex f B
- @kindex H f B
- @pindex calc-inc-beta
- @tindex betaI
- @tindex betaB
- The @kbd{f B} (@code{calc-inc-beta}) [@code{betaI}] command computes
- the incomplete beta function @cite{I(x,a,b)}. It is defined by
- @c{$I(x,a,b) = \left( \int_0^x t^{a-1} (1-t)^{b-1} dt \right) / B(a,b)$}
- @cite{betaI(x,a,b) = integ(t^(a-1) (1-t)^(b-1), t, 0, x) / beta(a,b)}.
- Once again, the @kbd{H} (hyperbolic) prefix gives the corresponding
- un-normalized version [@code{betaB}].
- X
- @kindex f e
- @kindex I f e
- @pindex calc-erf
- @tindex erf
- @tindex erfc
- The @kbd{f e} (@code{calc-erf}) [@code{erf}] command computes the
- error function @c{$\hbox{erf}(x) = {2 \over \sqrt{\pi}} \int_0^x e^{-t^2} dt$}
- @cite{erf(x) = 2 integ(exp(-(t^2)), t, 0, x) / sqrt(pi)}.
- The complementary error function @kbd{I f e} (@code{calc-erfc}) [@code{erfc}]
- is the corresponding integral from @samp{x} to infinity; the sum
- @c{$\hbox{erf}(x) + \hbox{erfc}(x) = 1$}
- @cite{erf(x) + erfc(x) = 1}.
- X
- @kindex f j
- @kindex f y
- @pindex calc-bessel-J
- @pindex calc-bessel-Y
- @tindex besJ
- @tindex besY
- The @kbd{f j} (@code{calc-bessel-J}) [@code{besJ}] and @kbd{f y}
- (@code{calc-bessel-Y}) [@code{besY}] commands compute the Bessel
- functions of the first and second kinds, respectively.
- In @samp{besJ(n,x)} and @samp{besY(n,x)} the ``order'' parameter
- @cite{n} is often an integer, but is not required to be one.
- Calc's implementation of the Bessel functions currently limits the
- precision to 8 digits, and may not be exact even to that precision.
- Use with care!@refill
- X
- @node Branch Cuts, Random Numbers, Advanced Math Functions, Scientific Functions
- @section Branch Cuts and Principal Values
- X
- @noindent
- @cindex Branch cuts
- @cindex Principal values
- All of the logarithmic, trigonometric, and other scientific functions are
- defined for complex numbers as well as for reals.
- This section describes the values
- returned in cases where the general result is a family of possible values.
- Calc follows section 12.5.3 of Steele's @dfn{Common Lisp, the Language},
- second edition, in these matters. This section will describe each
- function briefly; for a more detailed discussion (including some nifty
- diagrams), consult Steele's book.
- X
- Note that the branch cuts for @code{arctan} and @code{arctanh} were
- changed between the first and second editions of Steele. Versions of
- Calc starting with 2.00 follow the second edition.
- X
- The new branch cuts exactly match those of the HP-28/48 calculators.
- They also match those of Mathematica 1.2, except that Mathematica's
- @code{arctan} cut is always in the right half of the complex plane,
- and its @code{arctanh} cut is always in the top half of the plane.
- Calc's cuts are continuous with quadrants I and III for @code{arctan},
- or II and IV for @code{arctanh}.
- X
- Note: The current implementations of these functions with complex arguments
- are designed with proper behavior around the branch cuts in mind, @emph{not}
- efficiency or accuracy. You may need to increase the floating precision
- and wait a while to get suitable answers from them.
- X
- For @samp{sqrt(a+bi)}: When @cite{a<0} and @cite{b} is small but positive
- or zero, the result is close to the @cite{+i} axis. For @cite{b} small and
- negative, the result is close to the @cite{-i} axis. The result always lies
- in the right half of the complex plane.
- X
- For @samp{ln(a+bi)}: The real part is defined as @samp{ln(abs(a+bi))}.
- The imaginary part is defined as @samp{arg(a+bi) = arctan2(b,a)}.
- Thus the branch cuts for @code{sqrt} and @code{ln} both lie on the
- negative real axis.
- X
- The following table describes these branch cuts in another way.
- If the real and imaginary parts of @cite{z} are as shown, then
- the real and imaginary parts of @cite{f(z)} will be as shown.
- Here @code{eps} stands for a small positive value; each
- occurrence of @code{eps} may stand for a different small value.
- X
- @smallexample
- X z sqrt(z) ln(z)
- ----------------------------------------
- X +, 0 +, 0 any, 0
- X -, 0 0, + any, pi
- X -, +eps +eps, + +eps, +
- X -, -eps +eps, - +eps, -
- @end smallexample
- X
- For @samp{z1^z2}: This is defined by @samp{exp(ln(z1)*z2)}.
- One interesting consequence of this is that @samp{(-8)^1:3} does
- not evaluate to @i{-2} as you might expect, but to the complex
- number @cite{(1., 1.732)}. Both of these are valid cube roots
- of @i{-8} (as is @cite{(1., -1.732)}); Calc chooses a perhaps
- less-obvious root for the sake of mathematical consistency.
- X
- For @samp{arcsin(z)}: This is defined by @samp{-i*ln(i*z + sqrt(1-z^2))}.
- The branch cuts are on the real axis, less than @i{-1} and greater than 1.
- X
- For @samp{arccos(z)}: This is defined by @samp{-i*ln(z + i*sqrt(1-z^2))},
- or equivalently by @samp{pi/2 - arcsin(z)}. The branch cuts are on
- the real axis, less than @i{-1} and greater than 1.
- X
- For @samp{arctan(z)}: This is defined by
- @samp{(ln(1+i*z) - ln(1-i*z)) / (2*i)}. The branch cuts are on the
- imaginary axis, below @cite{-i} and above @cite{i}.
- X
- For @samp{arcsinh(z)}: This is defined by @samp{ln(z + sqrt(1+z^2))}.
- The branch cuts are on the imaginary axis, below @cite{-i} and
- above @cite{i}.
- X
- For @samp{arccosh(z)}: This is defined by
- @samp{ln(z + (z+1)*sqrt((z-1)/(z+1)))}. The branch cut is on the
- real axis less than 1.
- X
- For @samp{arctanh(z)}: This is defined by @samp{(ln(1+z) - ln(1-z)) / 2}.
- The branch cuts are on the real axis, less than @i{-1} and greater than 1.
- X
- The following entries for @code{arcsin}, @code{arccos}, and
- @code{arctan} assume the current angular mode is radians. The
- hyperbolic functions operate independently of the angular mode.
- X
- @smallexample
- X z arcsin(z) arccos(z)
- -------------------------------------------------------
- X (-1..1), 0 (-pi/2..pi/2), 0 (0..pi), 0
- X (-1..1), +eps (-pi/2..pi/2), +eps (0..pi), -eps
- X (-1..1), -eps (-pi/2..pi/2), -eps (0..pi), +eps
- X <-1, 0 -pi/2, + pi, -
- X <-1, +eps -pi/2 + eps, + pi - eps, -
- X <-1, -eps -pi/2 + eps, - pi - eps, +
- X >1, 0 pi/2, - 0, +
- X >1, +eps pi/2 - eps, + +eps, -
- X >1, -eps pi/2 - eps, - +eps, +
- @end smallexample
- X
- @smallexample
- X z arccosh(z) arctanh(z)
- -----------------------------------------------------
- X (-1..1), 0 0, (0..pi) any, 0
- X (-1..1), +eps +eps, (0..pi) any, +eps
- X (-1..1), -eps +eps, (-pi..0) any, -eps
- X <-1, 0 +, pi -, pi/2
- X <-1, +eps +, pi - eps -, pi/2 - eps
- X <-1, -eps +, -pi + eps -, -pi/2 + eps
- X >1, 0 +, 0 +, -pi/2
- X >1, +eps +, +eps +, pi/2 - eps
- X >1, -eps +, -eps +, -pi/2 + eps
- @end smallexample
- X
- @smallexample
- X z arcsinh(z) arctan(z)
- -----------------------------------------------------
- X 0, (-1..1) 0, (-pi/2..pi/2) 0, any
- X 0, <-1 -, -pi/2 -pi/2, -
- X +eps, <-1 +, -pi/2 + eps pi/2 - eps, -
- X -eps, <-1 -, -pi/2 + eps -pi/2 + eps, -
- X 0, >1 +, pi/2 pi/2, +
- X +eps, >1 +, pi/2 - eps pi/2 - eps, +
- X -eps, >1 -, pi/2 - eps -pi/2 + eps, +
- @end smallexample
- X
- Finally, the following identities help to illustrate the relationship
- between the complex trigonometric and hyperbolic functions. They
- are valid everywhere, including on the branch cuts.
- X
- @smallexample
- sin(i*z) = i*sinh(z) arcsin(i*z) = i*arcsinh(z)
- cos(i*z) = cosh(z) arcsinh(i*z) = i*arcsin(z)
- tan(i*z) = i*tanh(z) arctan(i*z) = i*arctanh(z)
- sinh(i*z) = i*sin(z) cosh(i*z) = cos(z)
- @end smallexample
- X
- The ``advanced math'' functions (gamma, Bessel, etc.) are also defined
- for general complex arguments, but their branch cuts and principal values
- are not rigorously specified at present.
- X
- @node Random Numbers, Combinatorial Functions, Branch Cuts, Scientific Functions
- @section Random Numbers
- X
- @noindent
- @kindex k r
- @pindex calc-random
- @tindex random
- The @kbd{k r} (@code{calc-random}) [@code{random}] command produces
- random numbers of various sorts.
- X
- Given a positive numeric prefix argument @cite{M}, it produces a random
- integer @cite{N} in the range @c{$0 \le N < M$}
- @cite{0 <= N < M}. Each of the @cite{M}
- values appears with equal probability.@refill
- X
- With no numeric prefix argument, the @kbd{k r} command takes its argument
- from the stack instead. Once again, if this is a positive integer @cite{M}
- the result is a random integer less than @cite{M}. However, note that
- while numeric prefix arguments are limited to six digits or so, an @cite{M}
- taken from the stack can be arbitrarily large. If @cite{M} is negative,
- the result is a random integer in the range @c{$M < N \le 0$}
- @cite{M < N <= 0}.
- X
- If the value on the stack is a floating-point number @cite{M}, the result
- is a random floating-point number @cite{N} in the range @c{$0 \le N < M$}
- @cite{0 <= N < M}
- or @c{$M < N \le 0$}
- @cite{M < N <= 0}, according to the sign of @cite{M}.
- X
- If @cite{M} is zero, the result is a Gaussian-distributed random real
- number; the distribution has a mean of zero and a standard deviation
- of one. The algorithm used generates random numbers in pairs; thus,
- every other call to this function will be especially fast.
- X
- If @cite{M} is an error form @c{$m$ @code{+/-} $\sigma$}
- @samp{@var{m} +/- @var{s}} where @var{m}
- and @c{$\sigma$}
- @var{s} are both real numbers, the result uses a Gaussian
- distribution with mean @var{m} and standard deviation @c{$\sigma$}
- @var{s}.
- X
- If @cite{M} is an interval form, the lower and upper bounds specify the
- acceptable limits of the random numbers. If both bounds are integers,
- the result is a random integer in the specified range. If either bound
- is floating-point, the result is a random real number in the specified
- range. If the interval is open at enter end, the result will be sure
- not to equal that end value. (This makes a big difference for integer
- intervals, but for floating-point intervals it's relatively minor:
- with a precision of 6, @samp{random([1.0..2.0))} will return any of one
- million numbers from 1.00000 to 1.99999; @samp{random([1.0..2.0])} may
- additionally return 2.00000, but the probability of this happening is
- extremely small.)
- X
- If @cite{M} is a vector, the result is one element taken at random from
- the vector. All elements of the vector are given equal probabilities.
- X
- @vindex RandSeed
- The sequence of numbers produced by @kbd{k r} is completely random by
- default, i.e., the sequence is seeded each time you start Calc using
- the current time and other information. You can get a reproducible
- sequence by storing a particular ``seed value'' in the Calc variable
- @code{RandSeed}. Any integer will do for a seed; integers of from 1
- to 12 digits are good. If you later store a different integer into
- @code{RandSeed}, Calc will switch to a different pseudo-random
- sequence. If you ``unstore'' @code{RandSeed}, Calc will re-seed itself
- from the current time. If you store the same integer that you used
- before back into @code{RandSeed}, you will get the exact same sequence
- of random numbers as before.
- X
- @pindex calc-rrandom
- The @code{calc-rrandom} command produces a random real number between
- zero and one. It is equivalent to @samp{random(1.0)}.
- X
- @kindex k a
- @pindex calc-random-again
- The @kbd{k a} (@code{calc-random-again}) command produces another random
- number, re-using the most recent value of @cite{M}. With a numeric
- prefix argument @var{n}, it produces @var{n} more random numbers using
- that value of @cite{M}.
- X
- @kindex k h
- @pindex calc-shuffle
- @tindex shuffle
- The @kbd{k h} (@code{calc-shuffle}) command produces a vector of several
- random values with no duplicates. The value on the top of the stack
- specifies the set from which the random values are drawn, and may be any
- of the @cite{M} formats described above. The numeric prefix argument
- gives the length of the desired list. (If you do not provide a numeric
- prefix argument, the length of the list is taken from the top of the
- stack, and @cite{M} from second-to-top.)
- X
- If @cite{M} is a floating-point number, zero, or an error form (so
- that the random values are being drawn from the set of real numbers)
- there is little practical difference between using @kbd{k h} and using
- @kbd{k r} several times. But if the set of possible values consists
- of just a few integers, or the elements of a vector, then there is
- a very real chance that multiple @kbd{k r}'s will produce the same
- number more than once. The @kbd{k h} command produces a vector whose
- elements are always distinct. (Actually, there is a slight exception:
- If @cite{M} is a vector, no given vector element will be drawn more
- than once, but if several elements of @cite{M} are equal, they may
- each make it into the result vector.)
- X
- One use of @kbd{k h} is to rearrange a list at random. This happens
- if the prefix argument is equal to the number of values in the list:
- @kbd{[1, 1.5, 2, 2.5, 3] 5 k h} might produce the permuted list
- @samp{[2.5, 1, 1.5, 3, 2]}. As a convenient feature, if the argument
- @var{n} is negative it is replaced by the size of the set represented
- by @cite{M}. Naturally, this is allowed only when @cite{M} specifies
- a small discrete set of possibilities.
- X
- To do the equivalent of @kbd{k h} but with duplications allowed,
- given @cite{M} on the stack and with @var{n} just entered as a numeric
- prefix, use @kbd{v b} to build a vector of copies of @cite{M}, then use
- @kbd{V M k r} to ``map'' the normal @kbd{k r} function over the
- elements of this vector. @xref{Matrix Functions}.
- X
- @menu
- * Random Number Generator:: (Complete description of Calc's algorithm)
- @end menu
- X
- @node Random Number Generator, , Random Numbers, Random Numbers
- @subsection Random Number Generator
- X
- Calc's random number generator uses several methods to ensure that
- the numbers it produces are highly random. Knuth's @emph{Art of
- Computer Programming}, Volume II, contains a thorough description
- of the theory of random number generators and their measurement and
- characterization.
- X
- If @code{RandSeed} has no stored value, Calc calls Emacs' built-in
- @code{random} function to get a stream of random numbers, which it
- then treats in various ways to avoid problems inherent in the simple
- random number generators that many systems use to implement @code{random}.
- X
- When Calc's random number generator is first invoked, it ``seeds''
- the low-level random sequence using the time of day, so that the
- random number sequence will be different every time you use Calc.
- X
- Since Emacs Lisp doesn't specify the range of values that will be
- returned by its @code{random} function, Calc exercises the function
- several times to estimate the range. When Calc subsequently uses
- the @code{random} function, it takes only 10 bits of the result
- near the most-significant end. (It avoids at least the bottom
- four bits, preferably more, and also tries to avoid the top two
- bits.) This strategy works well with the linear congruential
- generators that are typically used to implement @code{random}.
- X
- If @code{RandSeed} contains an integer, Calc uses this integer to
- seed an ``additive congruential'' method (Knuth's algorithm 3.2.2A,
- computing @c{$X_{n-55} - X_{n-24}$}
- @cite{X_n-55 - X_n-24}). This method expands the seed
- value into a large table which is maintained internally; the variable
- @code{RandSeed} is changed from, e.g., 42 to the vector @cite{[42]}
- to indicate that the seed has been absorbed into this table. When
- @code{RandSeed} contains a vector, @kbd{k r} and related commands
- continue to use the same internal table as last time. There is no
- way to extract the complete state of the random number generator
- so that you can restart it from any point; you can only restart it
- from the same initial seed value. A simple way to restart from the
- same seed is to type @kbd{s r RandSeed} to get the seed vector,
- @kbd{v u} to unpack it back into a number, then @kbd{s t RandSeed}
- to reseed the generator with that number.
- X
- Calc uses a ``shuffling'' method as described in algorithm 3.2.2B
- of Knuth. It fills a table with 13 random 10-bit numbers. Then,
- to generate a new random number, it uses the previous number to
- index into the table, picks the value it finds there as the new
- random number, then replaces that table entry with a new value
- obtained from a call to the base random number generator (either
- the additive congruential generator or the @code{random} function
- supplied by the system). If there are any flaws in the base
- generator, shuffling will tend to even them out. But if the system
- provides an excellent @code{random} function, shuffling will not
- damage its randomness.
- X
- To create a random integer of a certain number of digits, Calc
- builds the integer three decimal digits at a time. For each group
- of three digits, Calc calls its 10-bit random number generator
- (which returns a value from 0 to 1023); if the random value is 1000
- or more, Calc throws it out and tries again until it gets a suitable
- value.
- X
- To create a random floating-point number with precision @var{p}, Calc
- simply creates a random @var{p}-digit integer and multiplies by
- @c{$10^{-p}$}
- @cite{10^-p}. The resulting random numbers should be very clean, but note
- that relatively small numbers will have few significant random digits.
- In other words, with a precision of 12, you will occasionally get
- numbers on the order of @c{$10^{-9}$}
- @cite{10^-9} or @c{$10^{-10}$}
- @cite{10^-10}, but those numbers
- will only have two or three random digits since they correspond to small
- integers times @c{$10^{-12}$}
- @cite{10^-12}.
- X
- To create a random integer in the interval @samp{[0 .. @var{m})}, Calc
- counts the digits in @var{m}, creates a random integer with three
- additional digits, then reduces modulo @var{m}. Unless @var{m} is a
- power of ten the resulting values will be very slightly biased toward
- the lower numbers, but this bias will be less than 0.1%. (For example,
- if @var{m} is 42, Calc will reduce a random integer less than 100000
- modulo 42 to get a result less than 42. It is easy to show that the
- numbers 40 and 41 will be only 2380/2381 as likely to result from this
- modulo operation as numbers 39 and below.) If @var{m} is a power of
- ten, however, the numbers should be completely unbiased.
- X
- The Gaussian random numbers generated by @samp{random(0.0)} use the
- ``polar'' method described in Knuth section 3.4.1C. This method
- generates a pair of Gaussian random numbers at a time, so only every
- other call to @samp{random(0.0)} will require significant calculations.
- X
- @node Combinatorial Functions, Probability Distribution Functions, Random Numbers, Scientific Functions
- @section Combinatorial Functions
- X
- @noindent
- Commands relating to combinatorics and number theory begin with the
- @kbd{k} key prefix.
- X
- @kindex k g
- @pindex calc-gcd
- @tindex gcd
- The @kbd{k g} (@code{calc-gcd}) [@code{gcd}] command computes the
- Greatest Common Divisor of two integers. It also accepts fractions;
- the GCD of two fractions is defined by taking the GCD of the
- numerators, and the LCM of the denominators. This definition is
- consistent with the idea that @samp{a / gcd(a,x)} should yield an
- integer for any @samp{a} and @samp{x}. For other types of arguments,
- the operation is left in symbolic form.@refill
- X
- @kindex k l
- @pindex calc-lcm
- @tindex lcm
- The @kbd{k l} (@code{calc-lcm}) [@code{lcm}] command computes the
- Least Common Multiple of two integers or fractions. The product of
- the LCM and GCD of two numbers is equal to the product of the
- numbers.@refill
- X
- @kindex k E
- @pindex calc-extended-gcd
- @tindex egcd
- The @kbd{k E} (@code{calc-extended-gcd}) [@code{egcd}] command computes
- the GCD of two integers @cite{x} and @cite{y} and returns a vector
- @cite{[g, a, b]} where @c{$g = \gcd(x,y) = a x + b y$}
- @cite{g = gcd(x,y) = a x + b y}.
- X
- @kindex !
- @pindex calc-factorial
- @tindex fact
- @tindex !
- The @kbd{!} (@code{calc-factorial}) [@code{fact}] command computes the
- factorial of the number at the top of the stack. If the number is an
- integer, the result is an exact integer. If the number is an
- integer-valued float, the result is a floating-point approximation. If
- the number is a non-integral real number, the generalized factorial is used,
- as defined by the Euler Gamma function. Please note that computation of
- large factorials can be slow; using floating-point format will help
- since fewer digits must be maintained. The same is true of many of
- the commands in this section.@refill
- X
- @kindex k d
- @pindex calc-double-factorial
- @tindex dfact
- The @kbd{k d} (@code{calc-double-factorial}) [@code{dfact}] command
- computes the ``double factorial'' of an integer. For an even integer,
- this is the product of even integers from 2 to @cite{N}. For an odd
- integer, this is the product of odd integers from 3 to @cite{N}. If
- the argument is an integer-valued float, the result is a floating-point
- approximation. This function is undefined for negative even integers.
- The notation @cite{N!!} is also recognized for double factorials.@refill
- X
- @kindex k c
- @pindex calc-choose
- @tindex choose
- The @kbd{k c} (@code{calc-choose}) [@code{choose}] command computes the
- binomial coefficient @cite{N}-choose-@cite{M}, where @cite{M} is the number
- on the top of the stack and @cite{N} is second-to-top. If both arguments
- are integers, the result is an exact integer. Otherwise, the result is a
- floating-point approximation. The binomial coefficient is defined for all
- real numbers by @c{$N! \over M! (N-M)!\,$}
- @cite{N! / M! (N-M)!}.
- X
- @kindex H k c
- @pindex calc-perm
- @tindex perm
- @ifinfo
- The @kbd{H k c} (@code{calc-perm}) [@code{perm}] command computes the
- number-of-permutations function @cite{N! / (N-M)!}.
- @end ifinfo
- @iftex
- The @kbd{H k c} (@code{calc-perm}) [@code{perm}] command computes the
- number-of-perm\-utations function @c{$N! \over (N-M)!\,$}
- @cite{N! / (N-M)!}.
- @end iftex
- X
- @kindex k b
- @kindex H k b
- @pindex calc-bernoulli-number
- @tindex bern
- The @kbd{k b} (@code{calc-bernoulli-number}) [@code{bern}] command
- computes a given Bernoulli number. The value at the top of the stack
- is a nonnegative integer @cite{n} that specifies which Bernoulli number
- is desired. The @kbd{H k b} command computes a Bernoulli polynomial,
- taking @cite{n} from the second-to-top position and @cite{x} from the
- top of the stack. If @cite{x} is a variable or formula the result is
- a polynomial in @cite{x}; if @cite{x} is a number the result is a number.
- X
- @kindex k e
- @kindex H k e
- @pindex calc-euler-number
- @tindex euler
- The @kbd{k e} (@code{calc-euler-number}) [@code{euler}] command similarly
- computes an Euler number, and @kbd{H k e} computes an Euler polynomial.
- Bernoulli and Euler numbers occur in the Taylor expansions of several
- functions.
- X
- @kindex k s
- @kindex H k s
- @pindex calc-stirling-number
- @tindex stir1
- @tindex stir2
- The @kbd{k s} (@code{calc-stirling-number}) [@code{stir1}] command
- computes a Stirling number of the first kind@c{ $n \brack m$}
- @asis{}, given two integers
- @cite{n} and @cite{m} on the stack. The @kbd{H k s} [@code{stir2}]
- command computes a Stirling number of the second kind@c{ $n \brace m$}
- @asis{}. These are
- the number of @cite{m}-cycle permutations of @cite{n} objects, and
- the number of ways to partition @cite{n} objects into @cite{m}
- non-empty sets, respectively.
- X
- @kindex k p
- @pindex calc-prime-test
- @cindex Primes
- The @kbd{k p} (@code{calc-prime-test}) command checks if the integer on
- the top of the stack is prime. For integers less than eight million, the
- answer is always exact and reasonably fast. For larger integers, a
- probabilistic method is used (See Knuth vol. II, section 4.5.4, algorithm P).
- The number is first checked against small prime factors (up to 13). Then,
- any number of iterations of the algorithm are performed. Each step either
- discovers that the number is non-prime, or substantially increases the
- SHAR_EOF
- true || echo 'restore of calc.texinfo failed'
- fi
- echo 'End of part 42'
- echo 'File calc.texinfo is continued in part 43'
- echo 43 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-